// pages/invite_code/invite_code.js
// 获取应用实例
const app = getApp()
var baseurl = app.baseurl.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuming: app.yuming.url,
    code: '',
    avater: "/media/zs.png", //需要https图片路径
    flag: true,
    tempFilePath:'' // 本地二维码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      icon: "loading",
      title: '加载中...',
      duration: 1000
    })
    let openid = wx.getStorageSync("openid")
    // 是否有openid登录
    if (openid != '') {
      // 列表
      wx.request({
        url: `${baseurl}/user.GetCode/create_code`,
        method: 'POST',
        data: {
          openid: wx.getStorageSync("openid"),
          user_id: wx.getStorageSync("user_id"),
        },
        success: (res) => {
          console.log(res.data);
          // 网络图片
          this.setData({
            code: res.data.data
          })

        }
      })


    } else {
      wx.reLaunch({
        url: '/pages/login/login'
      })
    }

  },
  share() {
    wx.showLoading({
      icon: "loading",
      title: '加载中...',
      duration: 1500
    })

    var that = this
    this.setData({
      flag: false
    })
    // 下载网络图片
    wx.downloadFile({
      url: this.data.code,
      success(res) {
        console.log('122', res);
        let tempFilePath = res.tempFilePath
        console.log('本地二维码：',tempFilePath);
        that.setData({
          tempFilePath: tempFilePath
        })

        // 分享二维码
        that.canvasImgFun()
    
      },
      fail: function (res) {
        console.log('下载网络图片失败', res)
        // fail(res)
      }
    })
  },

  // 分享二维码
  canvasImgFun() { //可用调用或在onLoad中直接调用
    // console.log('code2',this.data.code);
    // console.log('code2',this.data.tempFilePath);
    // console.log(wx.getStorageSync("user_name"));
    let _this = this
    const ctx = wx.createCanvasContext('myCanvas')
    ctx.rect(0, 0, 320, 400)
    ctx.setFillStyle('#ccc')
    ctx.fill()
    ctx.clip();//剪切
    ctx.drawImage('/img/bg.png', 0, 0, 320, 400)
    // 二维码
    ctx.drawImage(_this.data.tempFilePath, 95, 190, 120, 120, 0, 0)
    // ctx.drawImage('/img/banner.png', 90, 200, 120, 120, 0, 0)
    // ctx.drawImage('/img/banner.png', 100, 300, 140, 100, 0, 0)
    ctx.draw()
    ctx.beginPath()
    ctx.font = 'normal bold 50px Monospaced Number,Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif'
    ctx.setFillStyle('#fbdaa4')
    ctx.setFontSize(20)
    ctx.fillText(`您的好友邀请您进入新商城`, 40, 60)
    // ${wx.getStorageSync("user_name")}
    ctx.setFillStyle('#fff')
    ctx.setFontSize(14)
    ctx.fillText('长按识别小程序码，通过微信进入商城', 36, 360)
    ctx.font = 'normal bold 14px Monospaced Number,Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif'
    ctx.setFillStyle('#fbdaa4')
    ctx.setFontSize(26)
    ctx.fillText(` 可生食的鲜鸡蛋`, 60, 108)
    ctx.font = 'normal  14px Monospaced border 1px solid #ccc'
    ctx.setFillStyle('#fbdaa4')
    ctx.setFontSize(14)
    ctx.fillText(`一键购买，快速发货`, 90, 148)
    ctx.draw(true);

  },
  savecanvas() {
    let _this = this
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 320,
      height: 400,
      canvasId: 'myCanvas',
      success: function (res) {
        console.log(res, 'ssss')
        let img = res.tempFilePath
        wx.saveImageToPhotosAlbum({
          filePath: img,
          success(json) {
            wx.showToast({
              title: '成功保存,快去分享吧',
              icon: 'none',
              duration: 2000
            });
            _this.cancelcanvas()
            console.log(json, 'kk')
            this.setData({
              flag: true
            })
          },
          fail() {
            wx.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 2000
            });
            _this.cancelcanvas()
            this.setData({
              flag: true
            })
          }
        })
        console.log(res.tempFilePath)
      }
    })
  },
  // 分享二维码完成



})